/*
    Bandleader
    Copyright (c) 2006 by Grant Yoshida

    Licensed under the GPL version 2 or later,
    please see COPYING for details.
*/

#include "DeleteBar.h"
//
#include "ChordView.h"
#include "Document.h"

DeleteBar::DeleteBar(ChordView *view, int index, QUndoCommand *parent) :
    QUndoCommand("Delete Bar", parent),
    m_view(view),
    m_document(),
    m_index(index),
    m_deletedIndex(index),
    m_deletedBar()
{
    if (m_view)
        m_document = m_view->getDocument();
}

void DeleteBar::undo()
{
    if (m_document)
        m_document->insertChords(m_deletedIndex, m_deletedBar);

    if (m_view)
        m_view->setCursorPosition(m_index);
}

void DeleteBar::redo()
{
    if (m_document) {
        int barNumber = m_document->barNumberOf(m_index);
        m_deletedBar = m_document->chordsInBar(barNumber);

        m_document->deleteBar(m_index);

        m_deletedIndex = m_document->indexOf(barNumber);
    }

    if (m_view) {
        m_view->setCursorPosition(m_deletedIndex);
    }
}
